package none.rg.gantt.model;


import java.util.*;


/**
 * Class representing the task group. It provides means to store collection
 * of child tasks, add or remove them to this collection.
 * Group is technically a task with 0 duration, but allowed to last as long as necessary.
 * It also have internal SS as predecessor and FF as successor links to all of its children.
 * @author Rodion Gorkovenko
 */
public class Group extends Task {
    
    
    /** Mark for the task type. See {@link Task#SPECIAL_TYPE} */
    public static final String SPECIAL_TYPE = "group";
    
    private Set<Task> children = new HashSet<>();
    
    
    /**
     * Public constructor, same as for task but without duration (which is 0)
     * and "fixedDuration" flag (which is false).
     * @param chart Chart which holds the task.
     * @param title Title for the group
     */
    public Group(Chart chart, String title) {
        super(chart, title, 0, false);
    } // Group
    
    
    /** Adds existing task to the group. */
    public void includeChild(Task task) {
        children.add(task);
        getChart().addLink(this, task, Link.Type.START_START);
        getChart().addLink(task, this, Link.Type.FINISH_FINISH);
    } // includeChild
    
    
    /** Removes task from the group (task is not deleted) */
    public void excludeChild(Task task) {
        children.remove(task);
        task.removePredLink(this, Link.Type.START_START);
        this.removePredLink(task, Link.Type.FINISH_FINISH);
    } // excludeChild
    
    
    /** Checks whether the group have specified task as a child */
    public boolean haveChild(Task task) {
        return children.contains(task);
    } // haveChild
    
    
    /** Returns unmodifiable view of group's children */
    public Set<Task> viewChildren() {
        return Collections.unmodifiableSet(children);
    } // viewChildren
    
    
} // class Group
